<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{'min-height': mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <el-tooltip class="item" effect="dark" content="如果提示access_token错误，请点击该按钮" placement="top">
                            <el-button @click="retoken" type="primary" icon="fa fa-refresh"
                                       size="small"> 重置
                            </el-button>
                        </el-tooltip>
                        <el-badge :value="total" class="item" style="margin:0 15px">
                            <el-button size="small">库存数量</el-button>
                        </el-badge>
                        <el-badge :value="today_use_total" class="item" style="margin:0 15px">
                            <el-button size="small">有效推送</el-button>
                        </el-badge>
                        <el-badge :value="today_new_total" class="item" style="margin:0 15px">
                            <el-button size="small">今日新增</el-button>
                        </el-badge>
                    </div>
                    <el-table  v-loading="loading"
                               element-loading-text="努力加载中..."
                               element-loading-spinner="el-icon-loading"
                               :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="title" label="标题" width="200px"></el-table-column>
                        <el-table-column prop="template_id" label="模板ID" width="500px"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <el-button icon="fa fa-send"
                                           @click="push(scope.row)" type="primary" size="mini" plain> 推送
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <el-dialog
                :title="tmpDialogTitle"
                custom-class="bh-dialog"
                top="5vh"
                @open="resetForm('dataForm')"
                :visible.sync="tmpDialog"
                width="650px">
            <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="120px" class="demo-ruleForm">
                <el-form-item label="模板ID：" prop="template_id">
                    <el-input v-model="dataForm.template_id" disabled placeholder="请填写模板ID"></el-input>
                </el-form-item>
                <el-form-item label="跳转页：" prop="page">
                    <el-input v-model="dataForm.page" placeholder="请填写指定跳转页"></el-input>
                </el-form-item>
                <el-form-item
                        v-for="(item, index) in tmpDialogName"
                        :label="item"
                        :key="item.key"
                        :rules="{required: true, message: item+'不能为空', trigger: 'blur'}"
                >
                    <el-input v-model="dataForm.keywords[0][index]" :placeholder="item+'不能为空'"></el-input>
                </el-form-item>
                <el-form-item label="放大关键字">
                    <el-select v-model="dataForm.emphasis">
                        <el-option key="" label="无" value=""></el-option>
                        <el-option v-for="(item1, index1) in tmpDialogName" :key="index1" :label="item1" :value="index1"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="tmpDialog = false">取 消</el-button>
                <el-button type="primary" @click="update('dataForm')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {MessageList,MessageUpdate,MessagePush,reToken} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "模板消息"},],
                tableData:[],//列表数据
                tmpDialog:false,
                tmpDialogTitle:'',
                tmpDialogName:[],
                tmpDialogKey:[],
                //用户表单
                dataForm:{title:null,template_id:null,page:null,keywords:[],emphasis:null},
                rules: {
                    page: [{ required: true, message: '请指定跳转页', trigger: 'blur' }],
                },
                total:0,
                today_new_total:0,
                today_use_total:0,
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/message');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading'])
        },
        methods:{
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                MessageList().then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.list;
                        this.total = response.data.total;
                        this.today_new_total = response.data.today_new_total;
                        this.today_use_total = response.data.today_use_total;
                    }
                })
            },
            //推送
            push(info){
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要推送该模板消息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    MessagePush({id: info.template_id}).then((response)=>{
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message({
                                type: 'success',
                                duration: '1000',
                                message: response.msg,
                            });
                        }
                    });
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });
            },
            //编辑
            edit(info){
                this.tmpDialogTitle = info.title;
                this.tmpDialogName = info.keyword_text_arr;
                this.tmpDialogKey = info.keywords;
                this.dataForm = {title:info.title,template_id:info.template_id,page:info.page,keywords:info.form,emphasis:info.emphasis};
                this.tmpDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        MessageUpdate(this.dataForm).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.tmpDialog = false;
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
            // 重置token
            retoken(){
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要重置token吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    reToken().then((response)=>{
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message.success(response.msg);
                        }
                    })
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });
            },
        },
    }
</script>
